package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	/**
	 * ...
	 * @author hbb
	 */
	public class GameManager 
	{
		
		public var world:World;
		public var player:Player;
		public var interaction:Interaction;
		public var canvas:Sprite;
		
		private var process:Process;
		
		private var _tickedObjs:Array = [];
		
		public static const Singleton:GameManager = new GameManager( GameManagerLock );
		
		public function GameManager( lock:Class ) 
		{
			if ( lock !== GameManagerLock )
			{
				throw new ArgumentError('GameManager is a Singleton class');
			}
		}
		
		public function init( a_canvas:Sprite ):void
		{
			canvas = a_canvas;
			
			world = new World();
			player = new Player();
			interaction = new Interaction();
			interaction.onGesture = handleGesture;
			
			process = new Process();
			process.addTickedObj( world );
			process.addTickedObj( player );
			
			world.addObj( player, 50, 10 );
		}
		
		private function handleGesture( params:Object ):void 
		{
			var obj:*;
			var dx:Number, dy:Number;
			if ( 'nomove' == params.type )
			{
				obj = world.hitTest( params.x, params.y );
				
				if ( !obj ) return;
				
				if ( obj == player )
				{
					player.riseOrFall();
				}
				else
				{
					world.removeObj( obj );
				}
			}
			else if ( 'horimove' == params.type )
			{
				dx = params.x - params.startX;
				dy = params.y - params.startY;
				obj = new Ground( Math.abs( dx ), Math.abs( dy ) );
				world.addObj( obj, params.startX + dx * .5, params.startY + dy * .5 );
			}
			else if ( 'vertmove' == params.type )
			{
				dx = params.x - params.startX;
				dy = params.y - params.startY;
				obj = new Block( Math.abs( dx ), Math.abs( dy ) );
				world.addObj( obj, params.startX + dx * .5, params.startY + dy * .5 );
			}
		}
		
		public function start():void
		{
			process.start();
		}
		
	}

}

class GameManagerLock{}